﻿namespace ClassyBlog.Infrastructure
{
    using System;
    using System.Web.Mvc;
    using System.Web.Routing;

    public class RouteRegistrations
    {
        private readonly RouteCollection routes;

        public RouteRegistrations(RouteCollection routes)
        {
            if (routes == null)
            {
                throw new ArgumentNullException("routes");
            }

            this.routes = routes;
        }

        public virtual void Register()
        {
            routes.MapRoute(
                "contentproxy-get",
                "proxy",
                new { controller = "contentproxy", action = "get" });

            routes.MapRoute(
                "post-list",
                "posts/{page}/{year}/{month}/{day}",
                new
                    {
                        controller = "posts",
                        action = "index",
                        year = UrlParameter.Optional,
                        month = UrlParameter.Optional,
                        day = UrlParameter.Optional
                    },
                new
                    {
                        page = new PositiveIntegerConstraint(),
                        archivedDates = new ArchivedDatesConstraint()
                    });

            routes.MapRoute(
                "post-detail",
                "posts/{slug}",
                new { controller = "posts", action = "show" },
                new { slug = new PresenceConstraint() });

            routes.MapRoute(
                "tagged-post-list",
                "tagged/{page}/{slug}",
                new { controller = "posts", action = "tagged" },
                new
                    {
                        page = new PositiveIntegerConstraint(),
                        slug = new PresenceConstraint()
                    });

            routes.MapRoute(
                "page-list",
                "{page}/{*relativePath}",
                new
                    {
                        controller = "pages",
                        action = "index",
                        relativePath = UrlParameter.Optional
                    },
                new { page = new PositiveIntegerConstraint() });

            routes.MapRoute(
                "page-detail",
                "{*relativePath}",
                new { controller = "pages", action = "show" },
                new { relativePath = new PresenceConstraint() });

            routes.MapRoute(
                "default",
                "{controller}/{action}/{id}",
                new
                    {
                        controller = "home",
                        action = "index",
                        id = UrlParameter.Optional
                    });
        }
    }
}